<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

define("EMPTY", " - ");
class Category extends CI_Controller {

	//Doctrine EntityManager
	public $em;

	function __construct() {
		parent::__construct();

		//Not required if you autoload the library
		$this -> load -> library('doctrine');

		//Instantiate a Doctrine Entity Manager
		$this -> em = $this -> doctrine -> em;
		// $conn = $this->em->getConnection();
		//var_dump(models\service\CurrentUser::user());
		if (models\service\CurrentUser::user() == FALSE){
			header('location: ' . base_url());
		}

		// $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'varchar');
	}
	
	public function test(){
		$this->load->view('test');
	}

	// START Category
	public function index() {
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . "dashboard.php"),
								 array("label" => "Category", "link" => NULL)
								);
								
		$serv = new models\service\CategoryService;
					   
		// default search bar state
		$searchBar['fieldnames'] = array( array('label' => 'Code',
												'value' => 'Category.id'
												),
										  array('label' => 'Name',
												'value' => 'Category.name'
												)		
										);								
		$pFieldName[0] = "Category.id";
		$pFieldValue[0] = "";
		$pCondition[0] = "LIKE";
		$searchBy = FALSE;
		// end default search bar state
										
		if ($this->input->post('searchPar') != NULL){			
			$pFieldName=$this->input->post('fieldName');
			$pFieldValue=$this->input->post('fieldValue');
			$pCondition=$this->input->post('condition');
			
			if ($pFieldValue[0] != NULL){
				$searchBy = TRUE;
			}
		}
		
		$searchBar['pFieldName'] = $pFieldName;
		$searchBar['pCondition'] = $pCondition;
		$searchBar['pFieldValue'] = $pFieldValue;
		$data['searchBar'] = $searchBar;
		
		$categories = $serv->findCategoryByParentId(0);
		
		// echo "<pre>"; print_r($categories);die();
		$data['categories'] = $categories;
		$this -> load -> view('category/index', $data);		
	}
	
	public function createEdit($id="0"){
		$data['breadcrumb'] = array( array("label" => "Home", "link" => base_url() . "dashboard.php"),
								     array("label" => "Katalog", "link" => base_url() . "katalog"),
									 array("label" => "Add New Barang", "link" => NULL)
								);
		$data['id'] = $id;
		
		if ($id != "0"){
			// init the data
			$serv = new models\service\KatalogService;
			$barang=$serv->getBarangById($id);			
		} else {
			$barang=NULL;
		}
		$data['barang'] = $barang;
		$this->load->view('katalog/createEdit', $data);
	}
	
	public function getChild($id){
		$serv = new models\service\CategoryService;
		// {"title": "SubItem 1", "isLazy": true }, 	{"title": "SubFolder 2", "isFolder": true, "isLazy": true } 
		$categories = $serv->findCategoryByParentId($id);
		if (count($categories) > 0 && is_array($categories)){
			$i=0;
			foreach ($categories as $cat){
				$data[$i]['code'] = $cat->getId();
				$data[$i]['title'] = $cat->getName();
				$data[$i]['isLazy'] = true;
				$data[$i]['isFolder'] = true;
				$i++;
			}
			echo json_encode($data);	
		} else {
			$data=array();
			echo json_encode($data);
		}
	}
	
	public function save(){
		header('Content-type: application/json');
		$serv = new models\service\CategoryService;
		$param['parent'] = $this->input->post('parent');
		$param['name'] = $this->input->post('name');
		$param['id'] = $this->input->post('id');
		
		$criteria = array("name" => $param['name']);
		$categories = $serv->findBy($criteria);
		if (count($categories)>0){
			$data['error'] = 1;  
			$data['errorMsg'] = "Category name was existed";				
		} else {
			if ($this->input->post('level') != "0"){
				$param['level'] = $this->input->post('level');					
			}
			
			$category = $serv->save($param);
			if ($category!=NULL){
				$data['success'] = 1;
				$data['successMsg'] = "Category has been saved";	
			} else {
				$data['error'] = 1;  
				$data['errorMsg'] = "Operation Failed";
			}	
		}
		
		echo json_encode($data);
	}
	
	public function getById($id){
		header('Content-type: application/json');
		$serv = new models\service\CategoryService;
		$pcategory =array();
		if ($id>0){
			$category = $serv->findOneBy(array('id'=>$id));
			$pcategory['code'] = $category->getId();
			$pcategory['name'] = $category->getName();
			$pcategory['parent'] = $category->getParent()->getId();	
		}
		
		$data['category'] = $pcategory;
		echo json_encode($data);
	}
	
	public function remove($id,$force="FALSE"){
		header('Content-type: application/json');
		if ($id>0){
			$serv = new models\service\CategoryService;		
							
			$result = $serv->remove($id, $force);
			if ($result['result'] === TRUE){
				$data['success'] = 1;
				$data['successMsg'] = "Category has been removed"; 
			} else {
				$data['error'] = 1;
				$data['errorMsg'] = $result['message']." &nbsp;Click Button Force Delete";				
			}
			echo json_encode($data);
		}
	}
}

